# -*- coding: utf-8 -*-
# @Time: 2021/5/12 20:53
# @Author: moxinqian
# @Contact: tel 18382469064
# @File: demo-selenium-baidu.py
# @DESC: selenium执行百度搜索功能
from selenium import webdriver
from time import sleep

from selenium.webdriver.common.action_chains import ActionChains


#主站
host = "http://www.baidu.com"

#配置项
options = webdriver.FirefoxOptions()
#静默的方式打开浏览器
options.add_argument("--headless")
driver = webdriver.Firefox(executable_path="../firefox_driver/geckodriver_28.exe",firefox_options=options)


# 进入链接
driver.get(host)

sleep(3)

print(driver.title)
value = "Selenium"
# value = input("输入搜索内容：")

#向表单中填写输入的内容
driver.find_element_by_css_selector(
    '#kw') \
    .send_keys(value)

# 点击进入生活权益专区
ele = driver.find_element_by_css_selector(
    "#su")

driver.execute_script("arguments[0].click();", ele)

# 休眠，等待加载
sleep(3)

#打印10页搜索内容
for i in range(5):
    #翻页到最下方
    driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
    #获取节点并打印页面内容
    result = driver.find_element_by_css_selector("#content_left")
    print(driver.current_url)
    # print(result.text)
    # 找到下一页的标签
    next_page = driver.find_element_by_class_name("n:last-child")
    #点击一下页
    next_page.click()
    ActionChains(driver).double_click(driver.find_element_by_name("name")).perform()
    # ActionChains(driver).
    #等待加载
    sleep(3)

#关闭浏览器
driver.close()


